'''
Created on Apr 6, 2011

Generic Module for Different Types of Controlled Terminology

@author: glow
'''


from google.appengine.ext import db
from google.appengine.ext.db import polymodel

from django.conf import settings

class Organisation(db.Model):
  """
  Examples ISO, CDISC
  """
  name = db.StringProperty('Organisation Name')
  url = db.URLProperty('Link for Organisation', required=False)
  

class Controlled_Terminology_Source(db.Model):
  """
  Source for NCI Controlled Terminology
  """
  organisation = db.ReferenceProperty(Organisation, required=True)
  name = db.StringProperty('Source Name', required=True)
  description = db.StringProperty('Source Description', required=False)
  url = db.URLProperty('Source for Controlled Terminology (URL)', required=False)
  merge_date = db.DateProperty('Date of Merge', auto_now_add=True)
  user = db.UserProperty()
  file_name = db.StringProperty('Filename')

  
class CodedTerminology(polymodel.PolyModel):
  """
  Attempt to genericise all possible coded sources,
  In the codelist analogy, this would be the codelist name - it's the
  id for the collation of the 
  """
  name = db.StringProperty("Name")
  source = db.ReferenceProperty(Controlled_Terminology_Source, required=True)
  

# The next two classes are *very* NCI/CDISC centric   
class NCI_Controlled_Terminology_CodeListItem(db.Model):
  """
  This is interesting - one code can be in many codelists
  """
  # this should be a ManyToMany - subset of unit codelists for CDASH
  code = db.StringProperty('NCI Code')
  cdisc_submission_value = db.StringProperty('CDISC Submission Value', required=True)
  cdisc_synonyms = db.StringListProperty('CDISC Synonyms')
  cdisc_definition = db.TextProperty('CDISC Definition')
  nci_preferred_term = db.StringProperty('NCI Preferred Term')
  is_draft  = db.BooleanProperty('Draft Codelist', default=False)
  merge_date = db.DateProperty('Date of inclusion', auto_now_add=True)
  # need to add this after the fact
  codelists = db.ListProperty(db.Key)
  

class NCI_Controlled_Terminology_Codelist(CodedTerminology):
  code = db.StringProperty('NCI Code')
  extensible = db.BooleanProperty('Codelist is extensible?', default=True)
  codelist_name = db.StringProperty('CodeList Name')
  cdisc_submission_value = db.StringProperty('CDISC Submission Value', required=True)
  cdisc_synonyms = db.StringListProperty('CDISC Synonyms')
  cdisc_definition = db.TextProperty('CDISC Definition')
  nci_preferred_term = db.StringProperty('NCI Preferred Term')
  is_draft  = db.BooleanProperty('Draft Controlled Terminology', default=False)
  merge_date = db.DateProperty('Date of inclusion', auto_now_add=True)

  @property
  def coded_items(self):
    return [x for x in NCI_Controlled_Terminology_CodeListItem.gql("WHERE codelists = :1" , self.key())]
  

class SimpleDataType(CodedTerminology):
  # Organisation for the CT
  pass

# ISO 21090 data types
class AbstractDataType(CodedTerminology):
  """
  General Abstract DataType
  """
  attributes = db.ListProperty(db.Key)

class AbstractDataType_Attribute(db.Model):
  """
  A dependant Property
  """
  name = db.StringProperty('Attribute Name')
  description = db.StringProperty('Attribute Description')
  datatype = db.ReferenceProperty(SimpleDataType)
  
class AbstractDataType_Constraint(db.Model):
  """
  A dependant Property
  """
  adt = db.ReferenceProperty(AbstractDataType)
  constraint_item = db.ReferenceProperty(AbstractDataType_Attribute, 'Applies to')
  constraint_detail = db.StringProperty('Constraint')


  

  
